#pragma once
#include <iostream>
#include "oj_model_mysql.hpp"
// #include "oj_model.hpp"
#include <ctemplate/template.h>

namespace ns_view
{
    class View
    {
    public:
        View(){}
        ~View(){}
    public:
        void AllQuestions2html(const std::vector<ns_model::Question>& v, std::string* html)
        {

            // 创建词典
            ctemplate::TemplateDictionary root("all_questions"); // 根字典

            for(const auto& q : v)
            {
                ctemplate::TemplateDictionary* sub_dict = root.AddSectionDictionary("sub_question"); // 子字典
                sub_dict->SetValue("id", q.id);
                sub_dict->SetValue("name", q.name);
                sub_dict->SetValue("level", q.level);
            }

            // 获取html模板
            ctemplate::Template* tpl =  ctemplate::Template::GetTemplate(questions_path + "all_questions.html", ctemplate::DO_NOT_STRIP);
            tpl->Expand(html, &root); // 生成html
        }

        void OneQuestion2html(const ns_model::Question& q, std::string* html)
        {
            ctemplate::TemplateDictionary root("onequestion"); // 建立字典
            root.SetValue("id", q.id);
            root.SetValue("name", q.name);
            root.SetValue("level", q.level);
            root.SetValue("desc", q.desc);
            root.SetValue("head_code", q.header);

            // 获取html模板
            ctemplate::Template* tpl = ctemplate::Template::GetTemplate(questions_path + "one_question.html", ctemplate::DO_NOT_STRIP);
            tpl->Expand(html, &root);
        }
    private:
        const std::string questions_path = "./template_html/";
    };
}